﻿using System.Collections.Generic; 
using System.Linq; 
using DevExpress.Utils; 
using YDServices;
using YDModel;
using YDCommon;
using System;
using System.Windows.Forms;
using System.Drawing;

namespace YDWin.Customer
{
    public partial class CustomerForm : DevExpress.XtraEditors.XtraForm
    {

        List<T_Customers> opList = null;
        T_Customers item = null;
        CustomerService svr = new CustomerService();

        public CustomerForm()
        {
            InitializeComponent();


            Global.GirdViewSet(this.gvCustomer);
          
            //翻页事件
            pageControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);
    

        }
        /// <summary>
        /// 自动行号 自增长
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvCustomer_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }


        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadDataGrid();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadDataGrid()
        {
            WaitDialogForm sdf = new WaitDialogForm("", "正在查询......");
            item = null;
            ConditionItem[] conds = GetConditions();
            if (conds != null)
            {
                opList = svr.getList(conds);
                if (svr.HasError)
                {
                    Utils.ShowPrompt(MessageType.mtWarning, "载入失败\n" + svr.Errors);
                    svr.Errors.Clear();
                }
                else
                {
                    List<T_Customers> list = opList.Skip((pageControl1.PageIndex - 1) * pageControl1.PageSize).Take(pageControl1.PageSize).ToList();
                    pageControl1.DrawControl(opList.Count());
                    if (list.Count() == 0)
                    {
                        bsData.DataSource = null;
                    }
                    else
                    {
                        bsData.DataSource = list;
                        this.gvCustomer.MoveFirst();//移动到第一条记录
                        //UIChecked.BestFit();
                    }
                }
            }
            sdf.Close();

        }

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private ConditionItem[] GetConditions()
        {
            List<ConditionItem> conds = new List<ConditionItem>();

            conds.Add(new ConditionItem
            {
                PropertyName = "CustomerName",
                FindType = ConditionType.ctLike,
                Values = new object[] { txtCustomer.Text }
            }); 

            return conds.ToArray();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pagerControl1_OnPageChanged(object sender, EventArgs e)
        {
            LoadDataGrid();
        }
         

        /// <summary>
        /// 删除选中的记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        { 
            if (item == null) {
                Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
                return;
            }
            DialogResult dr = Utils.ShowPrompt(MessageType.mtInformation, "即将删除该规格信息,删除之后将不可恢复!", null, MessageBoxButtons.OKCancel);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            svr.DelItem(item); 
            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "删除失败\n" + svr.Errors);
                svr.Errors.Clear();
            }
            else
            {
                Utils.ShowPrompt(MessageType.mtInformation, "删除成功\n");
                LoadDataGrid();
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (item == null)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
                return;
            }
            AddCustomerForm form = new AddCustomerForm();
            form.Text = "编辑规格信息";
            form.state = "Edit";
            form.editTu = item;
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
            }
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvCustomer_Click(object sender, EventArgs e)
        { 
            item = bsData.Current as T_Customers; 
        }

        /// <summary>
        /// x新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddCustomerForm form = new AddCustomerForm();
            form.Text = "新增规格信息";
            form.state = "Add";
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
            }
        }
    }
}